package com.chuangyang.fixboxmaster.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chuangyang.fixboxmaster.AppData;
import com.chuangyang.fixboxmaster.R;
import com.chuangyang.fixboxmaster.bean.Part;
import com.chuangyang.fixboxmaster.listener.PartNumChangeListener;

import java.util.ArrayList;

/**
 * Created by Administrator on 2015/6/11.
 */
public class SelectPartListAdapter extends BaseAdapter {
    private ArrayList<Part> mParts;
    private PartNumChangeListener listener;
    public SelectPartListAdapter(ArrayList<Part> parts) {
        this.mParts = parts;
    }

    public void setNumberChangeListener(PartNumChangeListener listener) {
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return mParts == null ? 0 : mParts.size();
    }

    @Override
    public Part getItem(int position) {
        return mParts == null ? null : mParts.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(AppData.getContext()).inflate(R.layout.select_part_list_item, parent, false);
            holder.mTitle = (TextView) convertView.findViewById(R.id.part_title);
            holder.mNumControlLayout = (LinearLayout) convertView.findViewById(R.id.part_num_control);
            holder.mPartNumView = (TextView) convertView.findViewById(R.id.num);
            holder.mNumPlusView = (LinearLayout) convertView.findViewById(R.id.num_plus);
            holder.mNumReduceView = (LinearLayout) convertView.findViewById(R.id.num_reduce);
            holder.mPrice = (TextView) convertView.findViewById(R.id.part_price);
            holder.mArrow=(ImageView)convertView.findViewById(R.id.arrow_right);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final Part part = getItem(position);
        holder.mNumPlusView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                part.num++;
                holder.mPartNumView.setText("" + part.num);
                listener.onChange(part);
            }
        });

        holder.mNumReduceView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                part.num--;
                if (part.num <= 0)
                    part.num = 0;
                holder.mPartNumView.setText("" + part.num);
                listener.onChange(part);
            }
        });
        if (part != null) {
            if (part.title.equals("维修费") || part.title.equals("上门诊断费")) {
                holder.mNumControlLayout.setVisibility(View.INVISIBLE);
                holder.mArrow.setVisibility(View.VISIBLE);
            } else {
                holder.mNumControlLayout.setVisibility(View.VISIBLE);
                holder.mArrow.setVisibility(View.GONE);
            }
            holder.mPartNumView.setText("" + part.num);
            holder.mTitle.setText(part.title);
            holder.mPrice.setText("￥" + part.suggestPrice * part.num);
        }
        return convertView;
    }


    public static class ViewHolder {
        public TextView mTitle;
        public LinearLayout mNumControlLayout;
        public LinearLayout mNumPlusView;
        public TextView mPartNumView;
        public LinearLayout mNumReduceView;
        public TextView mPrice;
        public ImageView mArrow;
    }
}

